#!/bin/bash



LOAD_WARN=5.0
 
NETINFO="Network Info"

PBY='Powered by <a href="https://rampage2unixer-easydone.rhcloud.com">ailurus</a></a>' 
 
# font colours
RED='<font color="#ff0000">'
CYAN='<font color="#a1efe4">'
FONT='</font>'
START='<ul><li>'
END='</li></ul>'
NOW="$(date)"

# head #

writeHead(){
 echo '<HTML><HEAD><TITLE>Server Monitor</TITLE></HEAD>
 <BODY alink="#0066ff" bgcolor="#fdf6e3" link="#ae81ff" text="#0000ff" vlink="#0033ff">'
 echo '<CENTER><H1>'
 echo "$NETINFO</H1>"
 echo "Generated on $NOW"
 echo '</CENTER>'
 
}
 
# foot #

writeFoot(){
 echo "<HR><center>$PBY</center>"
  echo "</BODY></HTML>"
}

writeHead  
 
## main ##

for i in $(seq 11 20) $(seq 101 106)
do
  HOST=10.11.96.$i
  echo $HOST

  echo '<TABLE WIDTH=100% BORDER=2 BORDERCOLOR="#000080" CELLPADDING=4 CELLSPACING=4 FRAME=HSIDES RULES=NONE" >'
  echo '<TR VALIGN=TOP>'
  for host in $HOST
  do
    echo '<TD BGCOLOR="#fdf6e3">'
    SSH="ssh $host"
    HOSTNAME="$($SSH hostname)"
   
    UPTIME="$($SSH uptime)"
    if $(echo $UPTIME | grep -E "min|days" >/dev/null); then
      x=$(echo $UPTIME | awk '{ print $3 $4}')
    else
      x=$(echo $UPTIME | sed s/,//g| awk '{ print $3 " (hh:mm)"}')
    fi
    UPTIME="$RED $x $FONT"
   
    LOAD="$($SSH uptime |awk -F'average:' '{ print $2}')"
    x="$(echo $LOAD | sed s/,//g | awk '{ print $2}')"
    y="$(echo "$x >= $LOAD_WARN" | bc)"
    if [ "$y" == "1" ] 
      then 
        LOAD="$RED $LOAD (High) $FONT"  
      else
        LOAD="$RED $LOAD (OK) $FONT"
    fi
   
    CLOCK="$RED $($SSH date +"%r") $FONT"
  
    TOTALPROCESS="$RED $($SSH ps axue | grep -vE "^USER|grep|ps" | wc -l) $FONT"
  
    FS="$RED $($SSH df -h | grep opt | sed 's/ /\&nbsp;/g' | sed 's/$/\<BR\/g>') $FONT"
   
    MEM="$RED $($SSH free -m | sed 's/ /\&nbsp;/g' | sed 's/$/\<BR\>/g') $FONT"

    APPUSEDMEM="$RED $($SSH free -m | grep "buffers/cache": | awk '{ print $3 " MB" }') $FONT"
   
    ping -c1  $host>/dev/null
    if [ "$?" != "0" ] ; then
      PING="$CYAN Failed $FONT"   
    else
      PING="$RED OK $FONT"
  
      echo "<b><u>$HOSTNAME</u></b><BR>" 
      echo "Ping status: $PING<BR>"
      echo "Time: $CLOCK<BR>"
      echo "Uptime: $UPTIME <BR>"
      echo "Load avarage: $START $LOAD $END"
      echo "Total running process: $START $TOTALPROCESS $END"

      echo "Disk status:<BR>"
      echo "$FS<BR>"

      echo "MEM/SWAP status:<BR>" 
      echo "$MEM<BR>"  
      echo "$START AppUsed MEM: $APPUSEDMEM $END"
    fi   
   
    echo "</td>"
  done  
    echo "</tr></table>"

done

writeFoot
